import { _decorator, Component, Node, instantiate } from 'cc';
import { GameLaunch } from '../GameLaunch';
import { ResMgr } from '../Managers/ResMgr';
import { BasePanel } from '../Base/BasePanel';
import { DisObjUtil } from '../Utils/DisObjUtil';
import { Utilitys } from '../Utils/Utilitys';
const { ccclass, property } = _decorator;

@ccclass('PanelUtility')
export class PanelUtility extends Component {
    //
    public static Instance:PanelUtility = null;

    private UIList:Node = null;
    private PopList:Node = null;

    onLoad():void{
        if(null === PanelUtility.Instance){
            PanelUtility.Instance = this;
            this.UIList = GameLaunch.Instance.UIList;
            this.PopList = GameLaunch.Instance.PopList;
        }else{
            this.destroy();
            return;
        }
    }

    /**
     * 
     * @param abName RES_TYPE.GUI
     * @param resUrl Prefab_Type.UpSucceedUI
     * @returns 
     */
    public showPopLayout(abName:string, resUrl:string, isMode:boolean = false):Node{
        var prefabRes= ResMgr.Instance.getAsset(abName, resUrl);
        if(null != prefabRes){
            var prefabNode:Node = Utilitys.Instance.instantiate(prefabRes);
            isMode ? this.addModePanel(prefabNode, resUrl) : this.addPanel(prefabNode, resUrl);
            return prefabNode;
        }else{
            ResMgr.Instance.loadResByPrefab(abName, resUrl, (asset)=>{
                if(null != asset){
                    var prefabNode:Node = Utilitys.Instance.instantiate(asset);
                    isMode ? this.addModePanel(prefabNode, resUrl) : this.addPanel(prefabNode, resUrl);
                }
            })
            return null;
        }
        return null;
    }
    /**
     * add panel
     * @param panelNode 
     */
    public addPanel(panelNode:Node, nodeName:string):void{
        if(null != nodeName && nodeName.length > 0){
            panelNode.name = nodeName;
        }
        this.UIList.addChild(panelNode);
    }

    public addModePanel1(panelSprite:Node, nodeName:string):void{
        this.addModePanel(panelSprite, nodeName);
    }

    public addModePanel(panelSprite:Node, nodeName:string):void{
        if(null != nodeName && nodeName.length > 0){
            panelSprite.name = nodeName;
        }
        this.PopList.addChild(panelSprite);
    }

    public removeModeChild1(panelSprite:Node):void{
        panelSprite.removeFromParent();
    }

    public removeModeChild(panelSprite:Node):void{
        panelSprite.removeFromParent();
    }

    public removePanelByName(panelName:string):void{
        var panel:Node = this.PopList.getChildByName(panelName);
        if(null != panel){
            var basePanel = panel.getComponent(BasePanel);
            if(null != basePanel){
                basePanel.Dispose();
            }
            this.PopList.removeChild(panel);
            return;
        }
        //
        panel = this.UIList.getChildByName(panelName);
        if(null != panel){
            var basePanel = panel.getComponent(BasePanel);
            if(null != basePanel){
                basePanel.Dispose();
            }
            this.UIList.removeChild(panel);
            return;
        }
    }

    public getPanelByName(panelName:string):Node{
        var panel:Node = this.PopList.getChildByName(panelName);
        if(null != panel){
            return panel;
        }
        //
        panel = this.UIList.getChildByName(panelName);
        if(null != panel){
            return panel;
        }
        return null;
    }

    public top(panel:Node):void{
        DisObjUtil.toTop(panel);
    }

    public removeAllPanel():void{
        DisObjUtil.removeAllChildren(this.UIList);
    }
}

